package com.ljkj.blockpay.ui.activity;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import androidx.fragment.app.Fragment;

import com.google.android.material.bottomnavigation.BottomNavigationMenuView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.kongzue.baseokhttp.util.BaseOkHttp;
import com.kongzue.baseokhttp.util.Parameter;
import com.ljkj.blockpay.R;
import com.ljkj.blockpay.basedata.ActivityCollector;
import com.ljkj.blockpay.basedata.BaseFragmentActivity;
import com.ljkj.blockpay.basedata.BuildConfig;
import com.ljkj.blockpay.databinding.ActivityMainBinding;
import com.ljkj.blockpay.presenter.MainPresenter;
import com.ljkj.blockpay.presenter.inter.MainInter;
import com.ljkj.blockpay.ui.fragment.AssetFragment;
import com.ljkj.blockpay.ui.fragment.DealFragment;
import com.ljkj.blockpay.ui.fragment.MineFragment;
import com.ljkj.blockpay.util.KeyboardUtil;
import com.ljkj.blockpay.util.SaveModelToSPUtil;
import com.ljkj.blockpay.util.StatusBarUtil;
import com.luck.picture.lib.tools.ToastUtils;
import com.yxing.ScanCodeConfig;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseFragmentActivity<ActivityMainBinding, MainPresenter> implements MainInter.MainInterView {

    private List<Fragment> fragments = new ArrayList<>();

    private ArrayList<Integer> ids = new ArrayList<>();
     private long mBacktime;
    @Override
    public int initContentView() {
        return R.layout.activity_main;
    }

    @Override
    protected void onResume() {
        super.onResume();


        if (!BuildConfig.getToken().equals("")){
            BaseOkHttp.overallHeader = new Parameter()
                    .add("content-type", "application/json")
                    .add("transfer-encoding", "Identity")
                    .add("x-token", BuildConfig.getToken());

        }else{
            BaseOkHttp.overallHeader = new Parameter()
                    .add("content-type", "application/json")
                    .add("transfer-encoding", "Identity");
        }
    }

    @Override
    protected MainPresenter createPresenter() {
        return null;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        if (!BuildConfig.getToken().equals("")){
            BaseOkHttp.overallHeader = new Parameter()
                    .add("content-type", "application/json")
                    .add("transfer-encoding", "Identity")
                    .add("x-token", BuildConfig.getToken());

        }else{
            BaseOkHttp.overallHeader = new Parameter()
                    .add("content-type", "application/json")
                    .add("transfer-encoding", "Identity");
        }
        StatusBarUtil.setRootViewFitsSystemWindows(this, true);
        initTabView();
        KeyboardUtil.hideKeyboard(this);
        int id = getIntent().getIntExtra("id",0);
        if (id == 1){
            switchFragment(1);
            binding.tlBottomLayout.setSelectedItemId(R.id.tab_deal);
        }
    }

    private void initTabView() {

        int index = 0;
        switchFragment(index);
        ids.add(R.id.tab_asset);
        ids.add(R.id.tab_deal);
        ids.add(R.id.tab_mine);
        setlistener();
        clearToast();

    }



    private void setlistener() {
        binding.tlBottomLayout.setItemIconTintList(null);
        binding.tlBottomLayout.setOnNavigationItemSelectedListener(menuItem -> {
                    switch (menuItem.getItemId()) {
                        case R.id.tab_asset:
                            switchFragment(0);
                            break;
                        case R.id.tab_deal:
                            switchFragment(1);
                            break;
                        case R.id.tab_mine:
                            switchFragment(2);
                            break;
                    }

                    return true;
                }
        );

    }
    AssetFragment assetFragment;
    DealFragment dealFragment;
    MineFragment mineFragment;
    private void switchFragment(int position) {
        FragmentTransaction fragmentManager = getSupportFragmentManager().beginTransaction();
        if (assetFragment != null){
            fragmentManager.hide(assetFragment);
        }
        if (dealFragment != null){
            fragmentManager.hide(dealFragment);
        }
        if (mineFragment != null){
            fragmentManager.hide(mineFragment);
        }
        switch (position){
            case 0:
                if (assetFragment == null){
                    assetFragment = new AssetFragment();
                    fragmentManager.add(R.id.fl_layout,assetFragment);
                }else{
                    fragmentManager.show(assetFragment);
                }
                break;
            case 1:
                if (dealFragment == null){
                    dealFragment = new DealFragment();
                    fragmentManager.add(R.id.fl_layout,dealFragment);
                }else{
                    fragmentManager.show(dealFragment);
                }
                break;
            case 2:
                if (mineFragment == null){
                    mineFragment = new MineFragment();
                    fragmentManager.add(R.id.fl_layout,mineFragment);
                }else{
                    fragmentManager.show(mineFragment);
                }
                break;
        }
        fragmentManager.commitAllowingStateLoss();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("zdl","MainActivity====onActivityResult=");

        if (resultCode == RESULT_OK && requestCode == ScanCodeConfig.QUESTCODE && data != null) {
            Bundle extras = data.getExtras();
            //运行下我手机
            if (extras != null) {
                String code = extras.getString(ScanCodeConfig.CODE_KEY);
                ToastUtils.s(MainActivity.this,code);
                Log.d("zdl","MainActivity====="+code);
            } else {
                ToastUtils.s(MainActivity.this,"code");
                Log.d("zdl","MainActivity=====错误");
            }
        }
        //人呢 看看有毛病没 还是不行出来了呀 可以 页面的得看看

//        FragmentManager fragmentManager = getSupportFragmentManager();
//        for (int i = 0; i < fragments.size(); i++) {
//            Fragment fragment = fragments.get(i);
//            if (fragment == null){
//
//            } else {
//                handleResult(fragment, requestCode, resultCode, data);
//            }
//        }
    }
    //

    private void handleResult(Fragment fragment, int requestCode, int resultCode, Intent data) {
        fragment.onActivityResult(requestCode, resultCode, data);

    }

    private void clearToast() {
        for (int i = 0; i < ids.size(); i++) {
            binding.tlBottomLayout.getChildAt(0).findViewById(ids.get(i)).setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    return true;
                }
            });
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - mBacktime < 3000) {
                ActivityCollector.finishAll();
            } else {
                ToastUtils.s(getBaseContext(),"再按一次退出C-Pay");
                mBacktime = System.currentTimeMillis();
            }
            return true;
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}